package pointshop;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

/**
 * Created by Administrator on 2014/11/10.
 */
public class CommandListener  implements CommandExecutor {
    public Main plugin;
    public Store store=new Store(plugin);

    public CommandListener(Main plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (label.equalsIgnoreCase("djshop"))
        {
            if (!(sender instanceof Player))
            {
                sender.sendMessage("§9PointShop §f- §5控制台无法执行该指令");
                return true;
            }
            Player p = (Player)sender;
            if (args.length == 0)
            {
                p.sendMessage("§9PointShop §2-----§6帮助§2-----");
/*Cshop open §9打开商店面板");
Cshop add [x] [y] [价格] [描述]§9将你手中物品以某某价格上架到第x行y格");
Cshop remove [x] [y] §9删除第x行第y个商品");*/
                return true;
            }
            if (args.length == 1)
            {
                if (args[0].equalsIgnoreCase("open"))
                {

                        store.ShopOpen(p);
                        return true;

                }
            }
            if (args.length == 3)
            {
                if (args[0].equalsIgnoreCase("remove"))
                {
                    if ( (p.hasPermission("PointShop.admin")))
                    {
                        if ((args[1].matches("[0-9]")) && (args[2].matches("[0-9]")))
                        {
                            store.removeItem(p, Integer.parseInt(args[1]), Integer.parseInt(args[2]));
                            return true;
                        }
                        p.sendMessage("§9PointShop §f- §4参数只能为数字");
                        return true;
                    }
                    p.sendMessage("§9PointShop §f- §9你没有权限这样做");
                    return true;
                }
            }
            if (args.length == 6)
            {
                if ( p.hasPermission("PointShop.admin"))
                {
                    if (p.hasPermission("PointShop.add"))
                    {
                        if ((!args[1].matches("[0-9]")) || (!args[2].matches("[0-9]")))
                        {
                            p.sendMessage("§9PointShop §f- §4行数与格数只能为数字");
                            return true;
                        }
                        int x = Integer.parseInt(args[1]);
                        int y = Integer.parseInt(args[2]);
                        if ((x <1) || (y <1) || (x > 6) || (y > 9))
                        {
                            p.sendMessage("§9PointShop §f- §e参数超出范围");
                            return true;
                        }
                        ItemStack i = p.getItemInHand();
                        store.addItem(p, i, Integer.parseInt(args[3]), args[4], x, y,args[5]);
                        return true;
                    }
                }
            }
        }
        return true;
    }

}
